<?php
declare (strict_types = 1);

namespace app\controller\backend\home;

use app\service\logic\common\home\NoticeService;
use app\validate\common\rights\PickValidate;
use think\App;
use think\Request;
use think\Response;
use think\response\Json;
use app\exception\LogicException;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class NoticeController extends BackendController
{
    protected $service;

    public function __construct(App $app, NoticeService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 视图
     *
     * @return string
     */
    public function view(): string
    {
        return $this->layout('backend/home/notice', 'message_list');
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, PickValidate::class . '.index');
        $search['admin_id'] = $this->request->middleware('admin')['id'];

        $result = $this->service->getList($params, $search_fields, $search);
        return amis($result);
    }

    /**
     * @return Json
     */
    public function total()
    {
        $params = $this->request->param();
        $search_fields = ['admin_id', 'createtime'];
        $search = request()->only($search_fields);
        $search['admin_id'] = $this->request->middleware('admin')['id'];

        $total = $this->service->getCount($params, $search_fields, $search); // 全部
        array_push($search_fields, 'notice_category');
        $search['notice_category'] = 1;
        $category1Total = $this->service->getCount($params, $search_fields, $search); // 待办
        $search['notice_category'] = 2;
        $category2Total = $this->service->getCount($params, $search_fields, $search); // 通知
        $search['notice_category'] = 3;
        $category3Total = $this->service->getCount($params, $search_fields, $search); // 提前通知

        return amis([
            'total' => $total, // 全部
            'category_1_total' => $category1Total, // 待办
            'category_2_total' => $category2Total, // 通知
            'category_3_total' => $category3Total, // 提前通知
        ]);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $drop = $this->service->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $params = $request->only($this->service->getSaveColumn());
        $params['admin_id'] = $request->middleware('admin')['id'];
        $this->validate($params, PickValidate::class . '.save');
        $params['operator'] = $request->middleware('admin')['name'];

        $result = $this->service->create($params);

        return amis($result);
    }

    /**
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read(Request $request): Json
    {
        $params = $request->only(['id']);
        $result = $this->service->read($params);
        return amis($result);
    }

    /**
     * 保存更新的资源
     *
     * @param Request $request
     * @return Json
     */
    public function update(Request $request): Json
    {
        $params = $request->only($this->service->getUpdateColumn());
        $this->validate($params, PickValidate::class . '.update');
        $params['operator'] = $request->middleware('admin')['name'];

        $result = $this->service->update($params);

        if ($result) {
            return amis();
        } else {
            return amis([], '操作失败', 417);
        }
    }

    /**
     * 单项修改
     *
     * @param Request $request
     * @return Json
     */
    public function item(Request $request): Json
    {
        $params = $request->only($this->service->getUpdateColumn());
        $this->validate($params, PickValidate::class . '.item');
        $params['operator'] = $request->middleware('admin')['name'];

        $result = $this->service->item($params);

        if ($result) {
            return amis();
        } else {
            return amis([], '操作失败', 417);
        }
    }

    /**
     * 完成订单
     *
     * @param Request $request
     * @return Json
     */
    public function complete(Request $request)
    {
        $params = $request->only($this->service->getUpdateColumn());
        $this->validate($params, PickValidate::class . '.item');
        $params['type'] = 3;

        $result = $this->service->complete($params);
        return amis($result);
    }
}
